package com.dex4d.ui.mine.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.widget.TextView;
import android.widget.Toast;

import com.dex4d.R;
import com.dex4d.core.AppToolBarBaseActivity;
import com.dex4d.model.User;
import com.dex4d.utils.Commons;
import com.dex4d.utils.EthWalletUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by chengyuchun on 2018/11/5.
 */
public class ExportKeystoreActivity extends AppToolBarBaseActivity {
    @BindView(R.id.tv_keystore)
    TextView tv_keystore;

    @Override
    protected void initToolbar() {
        showTitle(getString(R.string.keystore_export));
        showBackView();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_export_keystore;
    }

    @Override
    protected void initVariables() {

    }

    @Override
    protected void loadData() {
        final String password = getIntent().getStringExtra(Commons.BUNDLE_KEYS.PWD_KEY);
        getLoadingManager().showLoadingDialog();
        new Thread(new Runnable() {
            @Override
            public void run() {
                final String keystore = EthWalletUtil.exportKeystore(password);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        getLoadingManager().dismissLoadingDialog();
                        tv_keystore.setText(keystore);
                    }
                });
            }
        }).start();
    }

    @OnClick(R.id.btn_copy)
    public void onViewClick(){
        copyKeystore();
        Toast.makeText(this, R.string.copy_keystore, Toast.LENGTH_LONG).show();

        User.getInstance().setBackup(true);
        User.getInstance().save();
    }

    private void copyKeystore() {
        ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData mClipData = ClipData.newPlainText("Label", tv_keystore.getText());
        clipboardManager.setPrimaryClip(mClipData);
    }
}
